package v1

import (
	"dolphin/sports/pkg/util"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
)

// 绑定为json
type UserLoginInfo struct {
	User     string `form:"userName" json:"userName" xml:"userName"  binding:"required"`
	Password string `form:"password" json:"password" xml:"password" binding:"required"`
}

func UserLogin(c *gin.Context) {

	var userinfo UserLoginInfo

	if err := c.ShouldBindJSON(&userinfo); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "token": ""})
		return
	}

	res, err := util.PassportLogin(userinfo.User, userinfo.Password)
	if err != nil {
		log.Fatal(err)
	}

	code := gjson.GetBytes(res, "code")

	if code.Int() == 0 {
		token := gjson.GetBytes(res, "data")
		c.JSON(http.StatusOK, gin.H{
			"msg":   "登录成功",
			"token": token.String(),
		})
		return
	} else {
		c.JSON(http.StatusBadRequest, gin.H{"msg": "登录失败"})
		return
	}
}
